package com.app.fenak.asynctasks;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.app.fenak.EmailActivity;
import com.app.fenak.RegisterActivity;
import com.app.fenak.dto.User;
import com.app.fenak.validation.ServicesUrls;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class RegisterAsyncTask extends AsyncTask<Object, Object, Boolean> {

	private RegisterActivity registerActivity;
	private User user;
	private Intent intent;

	@Override
	protected Boolean doInBackground(Object... arg0) {

		registerActivity = (RegisterActivity) arg0[0];
		user = (User) arg0[1];
		boolean success = false;

		try {

			ArrayList<NameValuePair> requestString = new ArrayList<NameValuePair>();
			requestString
					.add(new BasicNameValuePair(
							ServicesUrls.REGISTER_PARAM_FIRST_NAME, user
									.getFirstName()));
			requestString.add(new BasicNameValuePair(
					ServicesUrls.REGISTER_PARAM_LAST_NAME, user.getLastName()));
			requestString.add(new BasicNameValuePair(
					ServicesUrls.REGISTER_PARAM_EMAIL, user.getEmail()));
			requestString.add(new BasicNameValuePair(
					ServicesUrls.REGISTER_PARAM_USERNAME, user.getUserName()));
			requestString.add(new BasicNameValuePair(
					ServicesUrls.REGISTER_PARAM_PASSWORD, user.getPassword()));

			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(ServicesUrls.REGISTER_URL);

			httpPost.setEntity(new UrlEncodedFormEntity(requestString));
			Log.i("request ", httpPost.getRequestLine().toString());

			HttpResponse response = httpClient.execute(httpPost);
			Log.i("response", response.getStatusLine().toString());
			String resonseString = ServicesUrls.convertStreamToString(response
					.getEntity().getContent());
			Log.i("response ", resonseString);
			JSONObject jsonObject = new JSONObject(resonseString);
			success = jsonObject.getBoolean("post");
			Log.i(" register success", success + "");

		} catch (IOException e) {

			Log.e("IOException", e.getMessage());
			success = false;
		} catch (JSONException e) {

			Log.e("JSONException", e.getMessage());
			success = false;
		}

		return success;

	}

	@Override
	protected void onPostExecute(Boolean result) {

		super.onPostExecute(result);
		registerActivity.dismissProgressDialog();

		if (result) {
			intent = new Intent(registerActivity.getApplicationContext(),
					EmailActivity.class);
			registerActivity.startActivity(intent);
			registerActivity.finish();

		} else {

			registerActivity.finish();

		}

	}

}
